<?php

namespace App\Http\Controllers;


use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Tools\Func;
use Illuminate\Support\Facades\DB;

use App\Models\News;

use App\Models\NewsCategory;

class NewsController extends Controller
{
    use Func; 

    public function news(Request $request)
    {
       $data = News::select('id','title','category_id','created_at')->where(function($query) use($request){
		   if ($request->category_id) {
		       $query->where('category_id', $request->category_id);
		   }
	   })->orderBy('id', 'desc');
	   if ($request->type) {
		   $data = $data->paginate($request->limit ?? 10);
	   } else {
		   $data = $data->get();
	   }

       return $this->returnJson(0, 'success', $data);
    }   
	
	public function newsInfo(Request $request)
	{
	   
	   $data = News::where('id', $request->id)->first();
	
	   return $this->returnJson(0, 'success', $data);
	}  
	 
	 public function getCategory(Request $request)
	 {
	     $data = NewsCategory::orderBy('id', 'desc')->get();
	     return $this->returnJson(0, 'success', $data);
	 }

}
